package com.luoye_lj.dogcontroller.adapter;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class ListViewAdapter extends BaseAdapter {

	public static abstract class EditItem implements View.OnFocusChangeListener{
		String text;

		public EditItem(String text) {
			this.text = text;
		}

		@Override
		public abstract void onFocusChange(View v, boolean hasFocus);
	}

	public static abstract class ClickItem implements OnClickListener{
		int visibility;
		Drawable background;
		boolean enable;
		String text;

		public ClickItem() {
			visibility= ViewGroup.VISIBLE;
			enable=true;
		}

		public ClickItem(int visibility, boolean enable) {
			this.visibility = visibility;
			this.enable = enable;
		}

		public ClickItem(Drawable background, boolean enable) {
			this.background = background;
			this.enable = enable;
			visibility= ViewGroup.VISIBLE;
		}

		public ClickItem(int visibility, Drawable background, boolean enable) {
			this.visibility = visibility;
			this.background = background;
			this.enable = enable;
		}

		public ClickItem(int visibility, Drawable background, boolean enable, String text) {
			this.visibility = visibility;
			this.background = background;
			this.enable = enable;
			this.text = text;
		}

		public ClickItem(Drawable background, boolean enable, String text) {
			visibility= ViewGroup.VISIBLE;
			this.background = background;
			this.enable = enable;
			this.text = text;
		}

		@Override
		public abstract void onClick(View view);
	}
	
	Activity contex=null;
	ListView listView=null;
	ArrayList<HashMap<String,Object>> list=null;
	int itemLayoutId=0;
	String[] hashKeys=null;
	int[] viewIds=null;
	
	
	public ListViewAdapter(Activity contex, ListView listView,
                           ArrayList<HashMap<String, Object>> list, int itemLayoutId,
                           String[] hashKeys, int[] viewIds) {
		super();
		this.contex = contex;
		this.listView = listView;
		this.list = list;
		this.itemLayoutId = itemLayoutId;
		this.hashKeys = hashKeys;
		this.viewIds = viewIds;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewgroup) {
		// TODO Auto-generated method stub
		ArrayList<View> viewList=null;
		if(view==null){
			view=contex.getLayoutInflater().inflate(itemLayoutId, null);
			viewList=new ArrayList<View>();
			for(int i=0;i<viewIds.length;i++){
				View itemView=view.findViewById(viewIds[i]);
				viewList.add(itemView);
			}
			view.setTag(viewList);
		}else{
			viewList=(ArrayList<View>) view.getTag();
		}
		if(viewList!=null){
			int i_vs=viewList.size();
			for(int i=0;i<i_vs;i++){
				View itemView=viewList.get(i);
				if(itemView instanceof TextView){
					if(itemView instanceof Button){
						ClickItem ci=(ClickItem) list.get(position).get(hashKeys[i]);
						if(ci==null){
							((Button) itemView).setVisibility(View.GONE);
						}else {
							if(ci.background!=null) {
								((Button) itemView).setBackground(ci.background);
							}
							if(ci.text!=null) {
								((Button) itemView).setText(ci.text);
							}
							((Button) itemView).setVisibility(ci.visibility);
							((Button) itemView).setEnabled(ci.enable);
							((Button) itemView).setOnClickListener(ci);
						}
					}else if(itemView instanceof EditText){
						EditText itemTextView=(EditText)itemView;
						EditItem ei=(EditItem) list.get(position).get(hashKeys[i]);
						itemTextView.setText(ei.text);
						itemTextView.setOnFocusChangeListener(ei);
					}else{
						TextView itemTextView=(TextView)itemView;
						itemTextView.setText((SpannableString)list.get(position).get(hashKeys[i]));
					}
				}else if(itemView instanceof ImageView){
					ImageView itemImageView=(ImageView)itemView;
					int imageId=(int) list.get(position).get(hashKeys[i]);
					itemImageView.setImageResource(imageId); // 设置 drawable 资源
				}else if (itemView instanceof LinearLayout){
					LinearLayout itemLl=(LinearLayout) itemView;
					int visibility=(int) list.get(position).get(hashKeys[i]);
					itemLl.setVisibility(visibility);
				}
			}
		}
		
		return view;
	}

}
